package com.longxi.tianjintianbao.widget;

import android.animation.AnimatorSet;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.animation.OvershootInterpolator;
import android.widget.ImageView;

import com.longxi.tianjintianbao.R;

@SuppressLint("AppCompatCustomView")
public class DragView extends ImageView {

    @SuppressWarnings("unused")
    private String TAG = DragView.class.getSimpleName();
    private int startX;
    private int startY;
    private static int DEFAULT_RADIO = 30;
    private int CIRCLEX;
    private int CIRCLEY;
    private ValueAnimator valueX;
    private ValueAnimator valueY;
    private AnimatorSet animSetXY;
    private int bubbleColor;

    private final static int STATE_IDLE = 1;//静止的状态
    private final static int STATE_DRAG_NORMAL = 2;//正在拖拽的状态
    private final static int STATE_UP_BACK = 5;//放手后的没有断裂的返回的状态
    private int CurrentState = STATE_IDLE;

    private Paint paint;

    public DragView(Context context) {
        this(context, null);
    }

    public DragView(Context context, AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public DragView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DragBubbleView, defStyleAttr, 0);
        int r = (int) a.getDimension(R.styleable.DragBubbleView_bubbleRadius, getResources().getDisplayMetrics().density * 20);
        bubbleColor = a.getColor(R.styleable.DragBubbleView_bubbleColor, Color.RED);
        setOriginR(r);
        a.recycle();
        initView();

    }


    private void initView() {
        animSetXY = new AnimatorSet();
        valueX = ValueAnimator.ofInt(startX, CIRCLEX);
        valueY = ValueAnimator.ofInt(startY, CIRCLEY);
        animSetXY.playTogether(valueX, valueY);
        valueX.setDuration(500);
        valueY.setDuration(500);
        valueX.setInterpolator(new OvershootInterpolator());
        valueY.setInterpolator(new OvershootInterpolator());
        valueX.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                startX = (int) animation.getAnimatedValue();
                invalidate();
            }

        });
        valueY.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                startY = (int) animation.getAnimatedValue();
                invalidate();

            }
        });
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(bubbleColor);

    }

    private void updatePath() {
        int dy = Math.abs(CIRCLEY - startY);
        int dx = Math.abs(CIRCLEX - startX);

        CurrentState = STATE_DRAG_NORMAL;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int cx = startX == 0 ? CIRCLEX : startX;
        int cy = startY == 0 ? CIRCLEY : startY;
        switch (CurrentState) {
            case STATE_IDLE://空闲状态，就画默认的圆
                canvas.drawCircle(CIRCLEX, CIRCLEY, DEFAULT_RADIO, paint);//默认的
                break;
            case STATE_UP_BACK://执行返回的动画
            case STATE_DRAG_NORMAL://拖拽状态 画贝塞尔曲线和两个圆
                canvas.drawCircle(cx, cy, DEFAULT_RADIO, paint);//拖拽的
                break;
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (!isEnabled()) {
            return true;
        }
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN://有事件先拦截再说！！
                getParent().requestDisallowInterceptTouchEvent(true);
                CurrentState = STATE_IDLE;
                animSetXY.cancel();
                startX = (int) ev.getX();
                startY = (int) ev.getY();
                break;
            case MotionEvent.ACTION_MOVE://移动的时候
                startX = (int) ev.getX();
                startY = (int) ev.getY();
                updatePath();
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                CurrentState = STATE_UP_BACK;
                valueX.setIntValues(startX, CIRCLEX);
                valueY.setIntValues(startY, CIRCLEY);
                animSetXY.start();
                break;
        }
        return true;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        CIRCLEX = (int) ((w) * 0.5 + 0.5);
        CIRCLEY = (int) ((h) * 0.5 + 0.5);
        CurrentState = STATE_IDLE;
    }

    public void setOriginR(int progress) {
        DEFAULT_RADIO = progress;
        requestLayout();
        invalidate();
    }

}
